package com.tencent.cloud.iov.util.backdoor.authority;

import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.tencent.cloud.iov.util.backdoor.DebugBackdoor;

import java.io.IOException;

import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 切换域名环境
 *
 * @author showxiao
 * @date 2018/10/11
 */
public class SwitchAuthorityInterceptor implements Interceptor {

    @Override
    public Response intercept(@NonNull Chain chain) throws IOException {
        Request request = chain.request();

        AuthorityEnum authority = DebugBackdoor.getAuthority();
        if (authority != null) {
            String scheme = authority.scheme;
            String host = authority.host;

            if (!TextUtils.isEmpty(scheme) && !TextUtils.isEmpty(host)) {
                HttpUrl httpUrl = request.url().newBuilder()
                        .scheme(scheme)
                        .host(host)
                        .build();

                request = request.newBuilder().url(httpUrl).build();
            }
        }

        return chain.proceed(request);
    }
}
